package com.example.testspringbootstater.autoconfigure;

import com.example.testspringbootstater.constants.IdGenConstants;
import com.example.testspringbootstater.properties.IdGenProperties;
import com.example.testspringbootstater.service.IdGen;
import com.example.testspringbootstater.service.impl.SnowFlakeIDGen;
import com.example.testspringbootstater.service.impl.UUIDGen;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: fan
 * @CreateTime 2024/7/24 14:14
 * @Description
 * @Version 1.0
 */
@Configuration
@EnableConfigurationProperties(IdGenProperties.class)
public class IdGenAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(IdGen.class) // 加上这个如果这个不满足条件，可以自定义
    @ConditionalOnProperty(prefix = "idgen", name = "type",havingValue = IdGenConstants.UUID ,matchIfMissing = true)
    public IdGen uuidGen(){
        return new UUIDGen();
    }

    @Bean
    @ConditionalOnMissingBean(IdGen.class)
    @ConditionalOnProperty(prefix = "idgen", name = "type", havingValue = IdGenConstants.SNOWFLAKE)
    public IdGen  snowFlakeIDGen(){
        return new SnowFlakeIDGen();
    }

}
